/*
 * Copyright 2009 Sid Stamm <sid@sidstamm.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Author:
 * Sid Stamm <sid@sidstamm.com>
 */

dump("FOO\n");

const DEBUG_CL_CHROME = true;

var CertLock = {
  //some debugging functions
  dump: function my_dump(x)
  {
    if(DEBUG_CL_CHROME) { dump(" CERTLOCK : " + x + "\n"); }
  },

  dump_props: function my_dump_props(x, showValues) 
  {
    for(let p in x) {
      if(showValues) {
        this.dump(" >> [" + p + "] = " + x[p]);
      } else {
        this.dump(" >> [" + p + "] ");
      }
    }
  },

  service: 
    Cc['@sidstamm.com/certlock;1'].getService(Ci.ssICertLock),

  QueryInterface: function(aIID)
  {
    if (aIID.equals(Ci.nsIWebProgressListener) ||
        aIID.equals(Ci.nsISupportsWeakReference) ||
        aIID.equals(Ci.nsISupports))
      return this;
    throw Components.results.NS_NO_INTERFACE;
  },

  init: function()
  {
    this.dump("init()");
    gBrowser.addProgressListener(this, Ci.nsIWebProgress.NOTIFY_ALL);
  },

  uinit: function()
  {
    this.dump("uinit()");
    gBrowser.removeProgressListener(this);
  },

  onStateChange: function(aWebProgress, aRequest, aFlag, aStatus)
  {
    // If you use this listener for more than one tab/window, use
    // aWebProgress.DOMWindow to obtain the tab/window which triggers the state change
    if(aFlag & Ci.nsIWebProgressListener.STATE_START)
    {
      // This fires when the load event is initiated
      if (aRequest.URI.schemeIs("https")) {
        this.dump(">>> STATE_START for    " + aRequest.URI.asciiSpec);
        this.dump("         securityInfo: " + aRequest.securityInfo);
        try {
          var provider = aRequest.QueryInterface(Ci.nsISecurityInfoProvider);
          this.dump("    provider info:     " + provider.securityInfo);
        } catch(e) {}
      }
    }
    if(aFlag & Ci.nsIWebProgressListener.STATE_STOP)
    {
      // This fires when the load finishes
    }
  },

  onLocationChange: function(aProgress, aRequest, aURI)
  {
    // This fires when the location bar changes; i.e load event is confirmed
    // or when the user switches tabs. If you use myListener for more than one tab/window,
    // use aProgress.DOMWindow to obtain the tab/window which triggered the change.

    // this is REALLY HACKY, but updates the URL bar to show the old URL
    // instead of the error page URL.
    if (aURI.schemeIs("about") && 
        aURI.asciiSpec.substring(5,16) == ":lockedcert") {
      var params = aURI.asciiSpec.substring(17).split("&");
      for each(var p in params) {
        if (p.substring(0,2) == "u=") {
          var ns = p.substring(2);
          gURLBar.value=ns;
          /*
          var ir = (aRequest.notificationCallbacks ?
                    aRequest.notificationCallbacks :
                    aRequest.loadGroup.notificationCallbacks)
                  .QueryInterface(Ci.nsIInterfaceRequestor);
          ir.getInterface(Ci.nsIDocShell).setCurrentURI(ns);
          */
        }
      }
    }
  },

  // For definitions of the remaining functions see related documentation
  onProgressChange: function(aWebProgress, aRequest, curSelf, maxSelf, curTot, maxTot) { },
  onStatusChange: function(aWebProgress, aRequest, aStatus, aMessage) { },
  onSecurityChange: function(aWebProgress, aRequest, aState) {
    if (aState & Ci.nsIWebProgressListener.STATE_IS_SECURE) {
      this.dump(">>> STATE_IS_SECURE for    " + aRequest.URI.asciiSpec);
      this.dump("         securityInfo: " + aRequest.securityInfo);
    }
  }
           
}

window.addEventListener("load", function() {CertLock.init()}, false);
window.addEventListener("unload", function() {CertLock.uninit()}, false);

